# -*- coding: utf-8 -*-
# @Time    : 2020/9/8 23:24
# @Author  : liupengfei
# @File    : serializers.py

from rest_framework import serializers
from apps.member.serializers import MemeberInfoAppSerializer
from apps.system.serializers import ImagesSerializer
from utils.serializersfield import DelayTimeCharField
from apps.project_case.models import House
from apps.member.models import MemberRecord
from .models import Circle, CircleComment


class CircleCommentSerializer(serializers.ModelSerializer):
    member = serializers.SerializerMethodField(read_only=True)
    is_like = serializers.SerializerMethodField()

    def get_is_like(self, obj):
        user = self.context['request'].user
        if user.is_authenticated:
            return MemberRecord.is_like(obj, user.member)
        return False

    def get_member(self, obj):
        return MemeberInfoAppSerializer(obj.member).data

    class Meta:
        model = CircleComment
        fields = '__all__'


class CircleHouseSerializer(serializers.ModelSerializer):
    class Meta:
        model = House
        fields = ('id', 'address')


class CircleAppSerializer(serializers.ModelSerializer):
    member = serializers.SerializerMethodField(read_only=True)
    house_addr = serializers.CharField(source="house.address", read_only=True)
    images = serializers.SerializerMethodField(read_only=True)

    is_like = serializers.SerializerMethodField()

    def get_is_like(self, obj):
        user = self.context['request'].user
        if user.is_authenticated:
            return MemberRecord.is_like(obj, user.member)
        return False

    def get_images(self, obj):
        return ImagesSerializer(obj.images, many=True).data

    def get_member(self, obj):
        return MemeberInfoAppSerializer(obj.member).data

    class Meta:
        model = Circle
        fields = '__all__'
